package jsyf.oa.service.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.task.SynchroESHToYF;
import jsyf.oa.dao.system.CompanyDao;
import jsyf.oa.dao.system.UserDao;
import jsyf.oa.dao.task.SignRemindDao;
import jsyf.oa.dao.wechat.service_company.TokenDao;
import jsyf.oa.entity.task.SignRemindEntity;
import jsyf.oa.facade.task.SignRemindFacade;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by y on 2018/5/3.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class SignRemindService implements SignRemindFacade{
    public static final String TODAY_BIRTHDAY_REMIND = "今天是我们的同事%1s的生日，让我们大家一起为他送上最真挚的祝福," +
            "并在以后的工作生活中彼此互帮互助，携手向前。";
    public static final String TODAY_BIRTHDAY = "一份祝福，一份真诚，在你这个特别的日子里，公司全体同仁给您送上生日最最真挚的祝福，祝您生日快乐家庭幸福！";
    private Logger logger = Logger.getLogger(SynchroESHToYF.class);
    @Autowired
    private SignRemindDao signRemindDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private TokenDao tokenDao;

    /**
     * 查询是否有当天签到、缺勤提醒记录
     * @return
     */
    @Override
    public JSONObject getByRemark(String remark, String Date,String state) {
        String condition=" WHERE REMARK='" + remark + "' AND REPORT_DATE='" + Date + "'AND STATE='" + state + "'";
        return signRemindDao.getByConditon(condition);
    }
    @Override
    public String add(String remark, String state) {
        SignRemindEntity signRemindEntity=new SignRemindEntity();
        signRemindEntity.setRemark(remark);
        signRemindEntity.setReportDate(new SimpleDateFormat("yyyy-MM-dd").format(signRemindDao.getCurrentTime()));
        signRemindEntity.setState(Integer.parseInt(state));
        if(signRemindDao.add(signRemindEntity)>0){
            return "1";
        }
        return "0";
    }
   public List<JSONObject> getCompanyList(String date,String companyId){
      return userDao.getBirthdayUsers(date,companyId);
   }
    public List<JSONObject> getBirthdayUsers(String date,String companyId) {
        return userDao.getBirthdayUsers(date,companyId);
    }

    public void birthdayRemind(){
        logger.info("开始执行");
       List<JSONObject> companyList=getCompanyList(getdate(0),"");
        StringBuilder name=new StringBuilder();
        StringBuilder ids=new StringBuilder();
       for(JSONObject company:companyList){
           ids.append("'");
           ids.append("2017121618514C22B68296F31AF8DDE2");//不给谈总发
           ids.append("'");
           ids.append(",");
           List<JSONObject> todayBirthdays=getBirthdayUsers(getdate(0),company.getString("companyId"));
           String userIds ="";
           if(todayBirthdays.size()>0){
               for (JSONObject o : todayBirthdays) {
                   name.append(",");
                   name.append(o.getString("name"));
                   ids.append("'");
                   ids.append(o.getString("id"));
                   ids.append("'");
                   ids.append(",");
                   JSONObject msgObject = new JSONObject();
                   msgObject.put("userIds", o.getString("id"));//object.getString("id")
                   msgObject.put("content", String.format(TODAY_BIRTHDAY));
                   tokenDao.sendTextMsg(JSON.toJSONString(msgObject));
               }
               userIds = ids.toString().substring(0, ids.toString().length() - 1);
               if (name.length() > 0) {
                   name.deleteCharAt(0);
               }
               List<JSONObject> remindUsers=userDao.getViewJSONListByCondition(" AND companyId=? AND jobState!=1 AND id NOT in("+userIds+") GROUP BY id",new Object[]{company.getString("companyId")});
               JSONObject msgObject = new JSONObject();
               for (JSONObject hrUser : remindUsers) {
                   msgObject.put("userIds", hrUser.getString("id"));
                   msgObject.put("content", String.format(TODAY_BIRTHDAY_REMIND, name));
                   tokenDao.sendTextMsg(JSON.toJSONString(msgObject));
               }
               System.out.println("name----"+name);
               System.out.println("ids----"+userIds);
               ids.setLength(0);
               name.setLength(0);
           }
       }
        logger.info("执行完毕");
    }
    public String getdate(int i) // //获取前后日期 i为正数 向后推迟i天，负数时向前提前i天
    {
        Date dat = null;
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, -i);
        dat = cd.getTime();
        SimpleDateFormat dformat = new SimpleDateFormat("MM-dd");
        return dformat.format(dat);
    }

}
